<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_deleter.class.php');



class Category_Del_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_category/');

		return $ret;
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);



		$parent_category_event = new Web_Event_Simple('parent_category', Tangra_Parameter_Method::GET, 'parent_category');

		$delete_event = new Web_Event_Simple_Int('delete', Tangra_Parameter_Method::GET, 'del');
		$delete_comp = new Web_Event_Composite('delete_comp');
		$delete_comp->add_wes($delete_event);
		$delete_comp->add_wes($parent_category_event);
		$this->wed->add_wed_event_action_pair($delete_comp, 'delete_event');

		$delete_confirmed_event = new Web_Event_Simple_Int('delete_confirmed', Tangra_Parameter_Method::GET, 'del_confirmed');
		$delete_confirmed_comp = new Web_Event_Composite('delete_confirmed_comp');
		$delete_confirmed_comp->add_wes($delete_confirmed_event);
		$delete_confirmed_comp->add_wes($parent_category_event);
		$this->wed->add_wed_event_action_pair($delete_confirmed_comp, 'delete_confirmed_event');

//		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete', Tangra_Parameter_Method::GET, 'del'), 'delete_event');
//		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete_confirmed', Tangra_Parameter_Method::GET, 'del_confirmed'), 'delete_confirmed_event');
	}


	protected function delete_event($params) {
		$category_id = $params['delete'];
		$parent_category = $params['parent_category'];

		$this->export('parent_category', $parent_category);

		$dbc = $this->get_dbc();

		$category = new TCMS_Category_DBC();
		if ($category->load_by_id($dbc, $category_id)) {
			$this->export('category', $category);

			$view = $this->get_view('default');
		} else {
			$view = $this->create_redirect_view('admin/modules/tangra_cms/category/categories.php');
		}

		return $view;
	}


	protected function delete_confirmed_event($params) {
		$category_id = $params['delete_confirmed'];
		$parent_category = $params['parent_category'];

		$this->export('parent_category', $parent_category);

		$dbc = $this->get_dbc();

		try {
			$dbc->start_trans();
			TCMS_Category_Deleter::delete_cascade($dbc, $category_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->create_redirect_view('admin/modules/tangra_cms/category/categories.php', array('parent_category' => $parent_category));

		return $view;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Unexpected parameter.');
	}
}
